/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.IOException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CopyItem
/*     */   extends RedoableOp
/*     */ {
/*  36 */   private Map<Integer, Integer> mDestIds = new HashMap();
/*  37 */   private Map<Integer, String> mDestUuids = new HashMap();
/*     */   private MailItem.Type type;
/*     */   private int mDestFolderId;
/*     */   private boolean mFromDumpster;
/*     */   
/*     */   public CopyItem() {
/*  43 */     super(MailboxOperation.CopyItem);
/*  44 */     this.type = MailItem.Type.UNKNOWN;
/*  45 */     this.mDestFolderId = 0;
/*     */   }
/*     */   
/*     */   public CopyItem(int mailboxId, MailItem.Type type, int folderId) {
/*  49 */     this();
/*  50 */     setMailboxId(mailboxId);
/*  51 */     this.type = type;
/*  52 */     this.mDestFolderId = folderId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setDest(int srcId, int destId, String destUuid)
/*     */   {
/*  59 */     this.mDestIds.put(Integer.valueOf(srcId), Integer.valueOf(destId));
/*  60 */     this.mDestUuids.put(Integer.valueOf(srcId), destUuid);
/*     */   }
/*     */   
/*     */   public int getDestId(int srcId) {
/*  64 */     Integer destId = (Integer)this.mDestIds.get(Integer.valueOf(srcId));
/*  65 */     return destId == null ? -1 : destId.intValue();
/*     */   }
/*     */   
/*     */   public String getDestUuid(int srcId) {
/*  69 */     return (String)this.mDestUuids.get(Integer.valueOf(srcId));
/*     */   }
/*     */   
/*     */   protected void setFromDumpster(boolean fromDumpster) {
/*  73 */     this.mFromDumpster = fromDumpster;
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/*  78 */     StringBuilder sb = new StringBuilder("type=").append(this.type);
/*  79 */     sb.append(", destFolder=").append(this.mDestFolderId);
/*  80 */     sb.append(", [srcId, destId, destUuid]=");
/*  81 */     for (Map.Entry<Integer, Integer> entry : this.mDestIds.entrySet()) {
/*  82 */       int srcId = ((Integer)entry.getKey()).intValue();
/*  83 */       sb.append('[').append(srcId).append(',').append(entry.getValue());
/*  84 */       sb.append(',').append((String)this.mDestUuids.get(Integer.valueOf(srcId))).append(']');
/*     */     }
/*  86 */     if (this.mFromDumpster)
/*  87 */       sb.append(", fromDumpster=").append(this.mFromDumpster);
/*  88 */     return sb.toString();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException
/*     */   {
/*  93 */     out.writeInt(-1);
/*  94 */     out.writeInt(-1);
/*  95 */     out.writeByte(this.type.toByte());
/*  96 */     out.writeInt(this.mDestFolderId);
/*  97 */     out.writeShort((short)-1);
/*  98 */     out.writeInt(this.mDestIds.size());
/*  99 */     for (Map.Entry<Integer, Integer> entry : this.mDestIds.entrySet()) {
/* 100 */       int srcId = ((Integer)entry.getKey()).intValue();
/* 101 */       out.writeInt(srcId);
/* 102 */       out.writeInt(((Integer)entry.getValue()).intValue());
/* 103 */       if (getVersion().atLeast(1, 37)) {
/* 104 */         out.writeUTF((String)this.mDestUuids.get(Integer.valueOf(srcId)));
/*     */       }
/*     */     }
/* 107 */     if (getVersion().atLeast(1, 30)) {
/* 108 */       out.writeBoolean(this.mFromDumpster);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in)
/*     */     throws IOException
/*     */   {
/* 115 */     int srcId = in.readInt();
/* 116 */     int destId = in.readInt();
/* 117 */     if ((srcId > 0) && (destId > 0)) {
/* 118 */       this.mDestIds.put(Integer.valueOf(srcId), Integer.valueOf(destId));
/*     */     }
/* 120 */     this.type = MailItem.Type.of(in.readByte());
/* 121 */     this.mDestFolderId = in.readInt();
/* 122 */     in.readShort();
/* 123 */     if (this.mDestIds.isEmpty()) {
/* 124 */       int count = in.readInt();
/* 125 */       for (int i = 0; i < count; i++) {
/* 126 */         srcId = in.readInt();
/* 127 */         this.mDestIds.put(Integer.valueOf(srcId), Integer.valueOf(in.readInt()));
/* 128 */         if (getVersion().atLeast(1, 37)) {
/* 129 */           this.mDestUuids.put(Integer.valueOf(srcId), in.readUTF());
/*     */         }
/*     */       }
/*     */     }
/* 133 */     if (getVersion().atLeast(1, 30)) {
/* 134 */       this.mFromDumpster = in.readBoolean();
/*     */     } else {
/* 136 */       this.mFromDumpster = false;
/*     */     }
/*     */   }
/*     */   
/*     */   public void redo() throws Exception
/*     */   {
/* 142 */     int mboxId = getMailboxId();
/* 143 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/*     */     
/* 145 */     int i = 0;int[] itemIds = new int[this.mDestIds.size()];
/* 146 */     for (Iterator i$ = this.mDestIds.keySet().iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/* 147 */       itemIds[(i++)] = id;
/*     */     }
/*     */     try {
/* 150 */       if (!this.mFromDumpster) {
/* 151 */         mbox.copy(getOperationContext(), itemIds, this.type, this.mDestFolderId);
/*     */       } else
/* 153 */         mbox.recover(getOperationContext(), itemIds, this.type, this.mDestFolderId);
/*     */     } catch (MailServiceException e) {
/* 155 */       if (e.getCode() == "mail.ALREADY_EXISTS") {
/* 156 */         mLog.info("Item is already in mailbox " + mboxId);
/* 157 */         return;
/*     */       }
/* 159 */       throw e;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/CopyItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */